<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_car_model extends CI_Model {

 
    function __construct()
    {
         parent::__construct();
    }
 
	function select_page($data = array(
			'username'=>''  //用户名
			,'carno'=>''  //车牌号
			,'start'=>0		//开始记录数
			,'length'=>0))	//长度
	{
		
		if($data['username'])
		{
			$this->db->like('user.username',$data['username']);
		}
		if($data['carno'])
		{
			$this->db->like('user_car.carno',$data['carno']);
		}
	    $this->db->from('user_car');
		$this->db->join('user', 'user_car.uid = user.uid', 'left');

		//查询总记录数
		$count = $this->db->count_all_results();

		$result['count'] = $count;
		/////////////////////////////////////////////////////////////////
		$this->db->select('user_car.*');
		$this->db->select('user.username');
		$this->db->select('base_carinfo.carname');
		$this->db->select('base_carbrand.brandname');
		$this->db->select('base_carseries.seriesname');
		if($data['username'])
		{
			$this->db->like('user.username',$data['username']);
		}
		if($data['carno'])
		{
			$this->db->like('user_car.carno',$data['carno']);
		}
	    $this->db->from('user_car');
		$this->db->join('user', 'user_car.uid = user.uid', 'left');
		$this->db->join('base_carinfo', 'user_car.carid = base_carinfo.carid', 'left');
	    $this->db->join('base_carbrand', 'user_car.brandid = base_carbrand.brandid', 'left');
		$this->db->join('base_carseries', 'user_car.seriesid = base_carseries.seriesid', 'left');

		if(intval($data['length'])<0)
		{
			$data['length'] =10;
		}
		if($data['start'])
		{
			$this->db->limit($data['length'],$data['start']);
		}
		else
		{
			$this->db->limit($data['length'],0);
		}
		$this->db->order_by("id", "desc"); 


		$query = $this->db->get();

		$result['list']=$query->result_array();
 
		return $result;
	}


	function select_by_id($id)
	{
		$this->db->where('user_car.id',$id);
		
		 $this->db->from('user_car');
		$this->db->join('user', 'user_car.uid = user.uid', 'left');
		$this->db->join('base_carinfo', 'user_car.carid = base_carinfo.carid', 'left');
	    $this->db->join('base_carbrand', 'user_car.brandid = base_carbrand.brandid', 'left');
		$this->db->join('base_carseries', 'user_car.seriesid = base_carseries.seriesid', 'left');
		$query = $this->db->get();
		$row = $query->row_array();
		return $row;
	}

	function update($id,$data=array(),$dataext=array())
	{
		$this->db->where('id', $id);
		$this->db->update('user_car', $data); 
	}

	function insert($data=array(),$dataext=array())
	{
		$result = $this->db->insert('user_car', $data); 
		return $result;
	}

	function delete($id)
	{
		$result =array('success'=>true);
		$this->db->where('id',$id);
		$result['data'] = $this->db->delete('user_car'); 
		return $result;
	}
}